//
//  MineViewController.m
//  ChengXinBao
//
//  Created by RongZhou on 17/2/13.
//  Copyright © 2017年 RongZhou. All rights reserved.
//



#import "MineViewController.h"
#import <AFNetworking.h>
#import "MineTableViewCell.h"
#import "LoginViewController.h"
#import "PersonalInforViewController.h"
#import "CollectionViewController.h"
typedef NS_ENUM(NSUInteger,CellType){
    UITableViewCellHeader = 0,
    UITableViewCellCollection,
    UITableViewCellExit,
};
@interface MineViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;

@end

@implementation MineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}
-(void)viewWillAppear:(BOOL)animated{
    self.title = @"个人主页";
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTableView];
}
-(void)viewDidLayoutSubviews{
    if ([_tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [_tableView setSeparatorInset:UIEdgeInsetsZero];
        
    }
    if ([_tableView respondsToSelector:@selector(setLayoutMargins:)])  {
        [_tableView setLayoutMargins:UIEdgeInsetsZero];
    }

}
-(void)createTableView{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight - 64) style:UITableViewStylePlain];
    self.automaticallyAdjustsScrollViewInsets = NO;
    [self.view addSubview:self.tableView];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    UIView *view = [[UIView alloc] init];
    self.tableView.tableFooterView = view;
    UINib *nib = [UINib nibWithNibName:@"MineTableViewCell" bundle:nil];
    [self.tableView registerNib:nib forCellReuseIdentifier:@"MineTableViewCell"];
    [self.tableView reloadData];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]) {
        return 3;
    }
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CellType cellType = indexPath.section;
    switch (cellType) {
        case UITableViewCellHeader:{
            MineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MineTableViewCell"];
            cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
            if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]) {
                cell.nameLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
            }
            return cell;
        }
            break;
        case UITableViewCellCollection:{
            static NSString *cellID = @"cellID";
            UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = @"我的收藏";
            cell.imageView.image = [UIImage imageNamed:@"iocn_grzx_choucang"];
            return cell;
        }
        case UITableViewCellExit:{
            static NSString *cellID = @"cellId";
            UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
            cell.accessoryType =  UITableViewCellAccessoryDisclosureIndicator;
            cell.textLabel.text = @"退出";
            cell.imageView.image = [UIImage imageNamed:@"user_setting"];
            return cell;
        }
        default:
            break;
    }
    
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == UITableViewCellHeader) {
        return 100;
    }else{
        return 44;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == UITableViewCellHeader) {
        if ([[NSUserDefaults standardUserDefaults] objectForKey:@"userName"]) {
            PersonalInforViewController *personVc = [[PersonalInforViewController alloc] init];
            [self.navigationController pushViewController:personVc animated:YES];
        }else{
            LoginViewController *logVc = [[LoginViewController alloc] init];
            [self presentViewController:logVc animated:YES completion:nil];
        }
    }else if (indexPath.section == UITableViewCellCollection){
        CollectionViewController *collectionVc = [[CollectionViewController alloc] init];
        [self.navigationController pushViewController:collectionVc animated:YES];
    }else{
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:@"userName"];
        [self createTableView];
        NSLog(@"你点击了退出");
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPat{
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]){
        [cell setSeparatorInset:UIEdgeInsetsZero];
    }
    if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]){
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
